package com.hcss.core.mvc;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.hcss.core.common.filemgt.dao.AnnexFileDao;
import com.hcss.core.common.filemgt.model.AnnexFileModel;
import com.hcss.core.common.permissions.model.UserInfo;
import com.hcss.core.util.SysDictionary;



@Service("baseService")
@Transactional
public class BaseService {
	@Autowired
	public BaseDao baseDao;
	@Autowired
	public AnnexFileDao annexFileDao;
	public final Logger logger = Logger.getLogger(BaseService.class);
	/**
	 * @Title: insertFile
	 * <p>附件表新增信息<p>
	 * @param file
	 * @return: void
	 */
	public int insertFile(File file,String buinessKey,int buinessId){
		AnnexFileModel fileModel = new AnnexFileModel();
		fileModel.setCreator(this.getUserInfo().getId());
		fileModel.setBuinessKey(buinessKey);
		fileModel.setBuinessId(buinessId);
		fileModel.setFileName(file.getName());
		fileModel.setFilePath(file.getPath().split("upload")[1].replaceAll("/", "\\"));
		fileModel.setFileType(file.getName().substring(file.getName().lastIndexOf(".")+1));
		fileModel.setFileSize(file.length());
		return annexFileDao.insertAnnex(fileModel);
	}
	/**
	 * 获取指定bean对象
	 * @param beanName
	 * @return
	 */
	protected Object getBean(String beanName) {
		return RequestContextUtils.getWebApplicationContext(((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest()).getBean(beanName);
	}
	/**
	 * 获取用户ID
	 * @return
	 */
	public UserInfo getUserInfo(){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return (UserInfo)request.getSession().getAttribute(SysDictionary.LOGIN_USERINFO);
	}
}
